package servlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import service.DataService;
import service.ErrorService;
import service.ValidateInput;
import dataDAOImpl.MySQLOperatorDAO;
import dataDAOInterfaces.DALException;
import dataDAOInterfaces.IOperatorDAO;


/**
 * Servlet implementation class LoginServlet
 */

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	ValidateInput validate = new ValidateInput();

	/**
	 * Default constructor. 
	 */

	public LoginServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String id, pass, logout;
		RequestDispatcher dispatcher;
		DataService data = new DataService();
		HttpSession session = request.getSession();

		if (!request.getMethod().equals("POST"))
		{
			dispatcher = request.getRequestDispatcher("Login.jsp");
			dispatcher.forward(request, response);
			logout = request.getParameter("logout");
			if (request.getParameterMap().containsKey("logout") && logout.equals("true"))
			{
				session.invalidate();
			}
		}
		else
		{
			id = request.getParameter("id");
			pass = request.getParameter("pass");

			if (!validate.validate(id, null, null, null, pass, false)) //Hvis felterne ikke er udfyldt korrekt udskrives der fejlbesked
			{
				ErrorService error = validate.createError();
				error.setError(validate.getError());
				request.setAttribute("error", error);
				dispatcher = request.getRequestDispatcher("Login.jsp");
				dispatcher.forward(request, response);
			} 
			else if (validate.validateCredentials(id, pass)) //Hvis adgangskode mathcher med ID
			{
				IOperatorDAO opr = new MySQLOperatorDAO();
				try
				{
					String role = opr.getOperator(Integer.parseInt(id)).getRole();
					session.setAttribute("user", role);
					data.setAreaaccess(role);
					session.setAttribute("areaaccess", data.getAreaaccess());
					if(session.getAttribute("user").equals("1")){
						dispatcher = request.getRequestDispatcher("/WEB-INF/admin/Default.jsp");
						dispatcher.forward(request, response);

					} else if(session.getAttribute("user").equals("2")){
						dispatcher = request.getRequestDispatcher("/WEB-INF/farma/Default.jsp");
						dispatcher.forward(request, response);

					} else if(session.getAttribute("user").equals("3")){
						dispatcher = request.getRequestDispatcher("/WEB-INF/vaerft/Default.jsp");
						dispatcher.forward(request, response);
					}
				}
				catch (DALException e)
				{
					System.out.println("Fejl: "+e);
				}
			} 
			else
			{
				ErrorService error = validate.createError();
				error.setError("<li>Forkert operatør ID eller adgangskode</li>");
				request.setAttribute("error", error);
				dispatcher = request.getRequestDispatcher("Login.jsp");
				dispatcher.forward(request, response);

			}
		}
	}
}
